home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15235 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: page.vip.best.com!user
  2. From: page@best.com (Chris Page)
  3. Newsgroups: alt.msdos.programmer,comp.lang.c,comp.lang.pascal.misc
  4. Subject: Re: typecasting preferences
  5. Date: Wed, 17 Apr 1996 17:35:12 -0700
  6. Organization: Don't Panic!
  7. Message-ID: <page-1704961735120001@page.vip.best.com>
  8. References: <4l020g$i9j@srvr1.engin.umich.edu> <3173E842.7D17@willows.com>
  9. NNTP-Posting-Host: page.vip.best.com
  10. X-Newsreader: Yet Another NewsWatcher 2.1.8
  11.  
  12. In article <3173E842.7D17@willows.com>, Tarang Deshpande
  13. <tarang@willows.com> wrote:
  14.  
  15. > HASDI RODZMANN HASHIM wrote:
  16. > > 
  17. > > If you have a choice, do you want the parethesis pair over the caster
  18. > > (C-style) or over the castee (Pascal-style)? eg...
  19. > > 
  20. > > a = (int)b; /* cast to int */
  21. > > a = int(b); /* allowable in C++ */
  22. > > 
  23. > I would prefer the Pascal style where the parenthesis go around the
  24. > thing being casted.  This is so that the cast looks like a function
  25. > and it then seems consistent when overriding the cast operator.
  26.  
  27. On the one hand, I prefer the "C" style cast because it is explicit that a
  28. cast is taking place, whereas the functional style requires you to know
  29. whether the function is a cast function or not. Since casting is an
  30. important thing to know about in a statically typed language (ignoring
  31. RTTI for this discussion), I prefer the emphasis on casting; I think it
  32. makes code more readable.
  33.  
  34. On the other hand, the functional style emphasizes that some function may
  35. actually be called, and that that function may do significantly more than,
  36. for example, sign extension. However, I was just reading "Item 5: Be wary
  37. of user-defined conversion functions" in Scott Meyers' More Effective C++,
  38. and he basically concludes that you should never define cast operator
  39. functions in a class because they can be called unexpectedly (unless, of
  40. course, you really really want to :-).
  41.  
  42. Instead of either solution, I prefer the Dylan approach: there is a
  43. function "as()" that takes a type parameter and an object parameter and
  44. returns an object of the specified type, created using the object
  45. parameter. It looks like this:
  46.  
  47.   foo := as(<type>, bar);
  48.  
  49. The thing I like about this is that "as()" is defined as part of the
  50. language (like, say "new" in C++), so you always know that you are
  51. performing a cast, but  it also indicates that a function is, or may, be
  52. called. Of course, C++ can't currently handle this approach because it
  53. doesn't have values that represent types. Perhaps RTTI provides this
  54. solution?
  55.  
  56. -- 
  57. Chris Page        | Internet chain letters, advertisements and SPAMs suck...
  58. Don't Panic!      | 
  59. page@best.com     |                   Cut it out! :-P
  60.